/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts.TablasEquivalencias;

import BD.BaseDatos;
import com.myapp.struts.Institucion.Institucion;
import java.sql.ResultSet;
import java.util.LinkedList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Andreina Wilhelm
 */
public class SeleccionarListaInstitucionesAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        // crea instancia de BD
        BaseDatos bd = new BaseDatos();

        try {
            // establece conexion con BD
            bd.establecerConexion();

            // se crea el query
            String query = "SELECT id, nombre, sede FROM institucion WHERE activo='true' ORDER BY nombre";

            // se ejecuta query
            ResultSet rs = bd.ejecutarQuery(query);

            // se crea lista
            LinkedList<Institucion> instList = new LinkedList<Institucion>();

            while (rs.next()) {
                Institucion inst = new Institucion();
                inst.setId(Integer.parseInt(rs.getString("id")));
                inst.setNombre(rs.getString("nombre"));
                inst.setSede(rs.getString("sede"));
                instList.add(inst);
            }

            // Se agrega la lista al form 
            SeleccionarListaInstitucionesForm slif = (SeleccionarListaInstitucionesForm) form;
            slif.setInstitucionesList(instList);

            // Se termina conexion con BD
            bd.terminarConexion();
            
            
            // Al cambiar de institucion las listas de las carreras, materias y tablas deben cambiar entonces elimino esos forms de la sesion
            HttpSession session = request.getSession();
            session.removeAttribute("SeleccionarListaCarrerasForm");
            session.removeAttribute("SeleccionarListaMateriasForm");
            session.removeAttribute("SeleccionarListaTablasEquivalenciaForm");                  

            return mapping.findForward(SUCCESS);

        } catch (Exception e) {
            System.out.println("ERROR: Problema al consultar la BD");
        }

        // Se termina conexion con BD
        bd.terminarConexion();
            
        return mapping.findForward(FAILURE);
    }
}
